const App = getApp();
const func = require('../../../utils/func');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 订单ID
        order_id: 0,
        // 订单子ID
        details_id: 0,
        // 订单商品ID
        product_id: 0,
        // 商家收货地址
        address: {},
        // 可选物流公司
        express: {},
        // 申请售后订单商品
        orderGoods: {},
        // 申请售后服务数据
        service: {},
        // 申请售后服务记录
        serviceLog: {},
        // 售后类型
        serviceType: {},
        // 售后申请提交数据
        submitForm: {},
        // 是否正在加载中
        isLoading: true,
        // 售后内容字数限制
        contentLength: 300,
    },

    // 是否允许提交
    submitDisable: false,

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        // 设置查询数据
        _this.setData({
            order_id: parseInt(options.order_id) > 0 ? parseInt(options.order_id) : 0,
            details_id: parseInt(options.details_id) > 0 ? parseInt(options.details_id) : 0,
            product_id: parseInt(options.product_id) > 0 ? parseInt(options.product_id) : 0,
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let _this = this;
        // 查询订单商品是否可申请售后
        _this.getOrderGoodsService();
    },

    onPullDownRefresh() {
        let _this = this;
        // 查询订单商品是否可申请售后
        _this.getOrderGoodsService();
        wx.stopPullDownRefresh();
    },

    // 查询订单商品是否可申请售后
    getOrderGoodsService: function() {
        let _this = this;
        let postData = {
            action: 'getService',
            order_id: _this.data.order_id,
            details_id: _this.data.details_id,
            product_id: _this.data.product_id
        };
        App._requestApi(_this, App.globalData.config.shopOrderServiceActionUrl, postData,
            function (res) {
                _this.setData({
                    isLoading: false,
                    address: res.data.address ? res.data.address : [],
                    express: res.data.express ? res.data.express : [],
                    orderGoods: res.data.orderGoods ? res.data.orderGoods : [],
                    service: res.data.service ? res.data.service : [],
                    serviceLog: res.data.serviceLog ? res.data.serviceLog : [],
                    serviceType: res.data.serviceType ? res.data.serviceType : [],
                });
                _this._initFormData(res.data.orderGoods);
            },
            function (res) {
                App.showError(res.msg, function() {
                    wx.navigateBack({delta: 1});
                });
            }
        );
    },

    // 初始化form数据
    _initFormData: function(orderGoods) {
        let _this = this;
        let submitForm = {};
        if (orderGoods) {
            submitForm = {
                content: '',
                order_id: orderGoods.order_id,
                details_id: orderGoods.details_id,
                product_id: orderGoods.product_id,
                service_type: orderGoods.service_type,
                refund_price: orderGoods.refund_price,
                refund_coupon: orderGoods.refund_coupon,
                refund_balance: orderGoods.refund_balance,
                refund_scores: orderGoods.refund_scores,
                refund_scores_num: orderGoods.refund_scores_num
            };
        }
        _this.setData({submitForm: submitForm});
    },

    // 服务类型
    switchServiceType(e) {
        // 获取数据
        let _this = this;
        let service_type = e.detail.value;
        let submitForm = _this.data.submitForm;
        // 处理数据
        submitForm['service_type'] = service_type;
        // 重载数据
        _this.setData({submitForm: submitForm});
    },

    // 问题描述
    contentInput: function(e) {
        // 获取数据
        let _this = this;
        let content = e.detail.value;
        let submitForm = _this.data.submitForm;
        // 处理数据
        let contentLength = 300 - content.length
        submitForm['content'] = content;
        // 重载数据
        _this.setData({
            submitForm: submitForm,
            contentLength: contentLength
        });
    },

    // 退款金额
    priceInput: function(e) {
        // 获取数据
        let _this = this;
        let productPrice = e.detail.value;
        let submitForm = _this.data.submitForm;
        // 商品本身的价格
        let goodsPrice = _this.data.orderGoods.refund_price;
        // 处理数据
        if (parseFloat(productPrice) > parseFloat(goodsPrice)) {
            submitForm['refund_price'] = goodsPrice;
        } else {
            submitForm['refund_price'] = productPrice;
        }
        // 重载数据
        _this.setData({submitForm: submitForm});
    },

    submitService: function() {
        // 获取数据
        let _this = this;
        let submitForm = _this.data.submitForm;

        // // 判断是否重复提交
        // if (_this.submitDisable === true) return false;
        // // 表单提交按钮设为禁用 (防止重复提交)
        // _this.submitDisable = true;

        // 请选择申请类型
        if (parseInt(submitForm.service_type) === 0) {
            _this.submitDisable = false;
            App.showError('请选择申请类型');
            return false;
        }
        let postData = {
            action: 'addService',
            submitForm: JSON.stringify(submitForm)
        };
        App._requestPost(_this, App.globalData.config.shopOrderServiceActionUrl, postData,
            function (res) {
                if (res.code === 1) {
                    App.showSuccess(res.msg, function() {
                        wx.navigateBack();
                    });
                } else {
                    App.showError(res.msg);
                }
            },
            function (res) {
                App.showError(res.msg);
            }
        );
    },
})